package com.github.slprj.slake.net;

/**
 * nio服务端配置
 *
 * @author YangXiaoLong
 * 2022/8/23 9:31
 */
public class NioServerConfig extends NioConfig {
    private int port;
    private int ioThreads;

    public NioServerConfig() {
        setIoThreads(calcIoThreads());
        setBizThreads(Runtime.getRuntime().availableProcessors() * 4);
        setBizQueueSize(5000);
        setName("SlNioServer");

        //set back pressure config , 设置压力测试的值
        setMaxOutRequests(10000);
        setMaxInRequests(20000);
        setMaxInBytes(512 * 1024 * 1024);

    }

    /**
     * 计算Io线程数逻辑
     *
     * @return {@link int }
     * @author YangXiaoLong
     * 2022/10/12 17:19
     */
    private int calcIoThreads() {
        //可用cpu
        int p = Runtime.getRuntime().availableProcessors();
        if (p <= 3) {
            return 1;
        } else if (p <= 6) {
            return 2;
        } else if (p <= 12) {
            return 3;
        } else if (p <= 24) {
            return 4;
        } else if (p <= 40) {
            return 5;
        } else if (p <= 64) {
            return 6;
        } else if (p <= 100) {
            return 7;
        } else {
            return 8;
        }
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public int getIoThreads() {
        return ioThreads;
    }

    public void setIoThreads(int ioThreads) {
        this.ioThreads = ioThreads;
    }
}
